package dam

import (
	"flag"
	"runtime/debug"

	"go-main/library/stat/metric"
)

var (
	metricKratosVersion = metric.NewCounterVec(&metric.CounterVecOpts{
		Namespace: "infra",
		Subsystem: "kratos",
		Name:      "version",
		Help:      "infra kratos version.",
		Labels:    []string{"name"},
	})
)

func init() {
	var version string
	if version = buildVersion("go-main"); version == "" {
		version = buildVersion("gitee.com/hxchjm/go-main")
	}
	metricKratosVersion.Inc(version)
}

func buildVersion(path string) string {
	buildInfo, ok := debug.ReadBuildInfo()
	if !ok {
		return ""
	}
	for _, d := range buildInfo.Deps {
		if d.Path == path {
			if d.Replace != nil {
				return d.Replace.Version
			}
			return d.Version
		}
	}
	return ""
}

func debugger() HandlerFunc {
	return func(c *Context) {
		buildInfo, _ := debug.ReadBuildInfo()
		info := map[string]interface{}{
			"build_info":           buildInfo,
			"command_line_options": flag.Args(),
		}
		c.JSON(info, nil)
	}
}
